package com.hzdh.account.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.poi.ss.formula.functions.T;

import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PinYinUtils {
	
	public static String UPPERCASE_TEXT = "D";
	public static String LOWERCASE_TEXT = "X";
	
	public static String getFirstSpell(String chinese) {
		return getFirstSpell(chinese, HanyuPinyinCaseType.LOWERCASE);
	}
	
	/**
	 * 获取首字母大小写
	 * @param chinese
	 * @param textTransform [D: 大写, X: 小写]
	 * @return
	 */
	public static String getFirstSpell(String chinese, String textTransform) {
		String firstSpell = null;
		if (UPPERCASE_TEXT.equals(textTransform)) {
			firstSpell = getFirstSpell(chinese, HanyuPinyinCaseType.UPPERCASE);
		}else if (LOWERCASE_TEXT.equals(textTransform)) {
			firstSpell = getFirstSpell(chinese, HanyuPinyinCaseType.LOWERCASE);
		}
		return firstSpell;
	}

	public static String getFirstSpell(String chinese, HanyuPinyinCaseType caseType) {
		StringBuffer pybf = new StringBuffer();
		char[] arr = chinese.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(caseType);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] > 128) {
				try {
					String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
					if (temp != null) {
						pybf.append(temp[0].charAt(0));
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pybf.append(arr[i]);
			}
		}
		return pybf.toString().replaceAll("\\W", "").trim();
	}

	/**
	 * 获取汉字串拼音，英文字符不变
	 * 
	 * @param chinese 汉字串
	 * @return 汉语拼音
	 */
	public static String getFullSpell(String chinese) {
		StringBuffer pybf = new StringBuffer();
		char[] arr = chinese.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] > 128) {
				try {
					pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pybf.append(arr[i]);
			}
		}
		return pybf.toString();
	}

	/**
	 * 按照中文首字母排序[jdk会将某些字读取成其他拼音，例如重庆:ZhongQing,建议输入拼音排序比较靠谱]
	 * 
	 * @param chacters 待排序的字符串数组
	 */
	public static List<String> sortFistChineseCharacters(String[] chacters) {
		Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
		List<String> list = Arrays.asList(chacters);
		Collections.sort(list, com);
		return list;
	}


	public static void main(String[] args) {
		String str = getFirstSpell("汇通");
		System.out.println(str);
	}
}
